(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dynaparm.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'modalService',
			'dynaparmService' ];
	function deleteController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, modalService, dynaparmService) {
		var ctrl = this, dynaparmInfo = $scope.$stateParams.params;
		ctrl.validatorConfig = validatorConfig;
		ctrl.dynaparm = {};
		ctrl.dynaparm.dynaparmSubCat = {};
		dynaparmService.initCtrlFunction(ctrl);
		
		ctrl.init = function() {
			var params = {
				parmId : dynaparmInfo.parmId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.paramValues.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.dynaparm.parmValues';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			authService.delete(params).then(function(response) {
				ctrl.dynaparm = response.data || dynaparmInfo; 
				ctrl.dynaparm.parmValues = ctrl.dynaparm.parmValues || [];
				ctrl.changeServiceCode(ctrl.dynaparm);
				ctrl.changeParmCat(ctrl.dynaparm);
				ctrl.changedSubCat(ctrl.dynaparm);
				ctrl.changeParmTypeSubFun(ctrl.dynaparm);
				ctrl.changeParmValueUitype(ctrl.dynaparm);
				ctrl.layoutConfig = layoutConfig;
			}, angular.noop);
			 
		};

		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.dynaparm.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.dynaparm).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};

		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.dynaparm.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.dynaparm.list',
					'state' : 'cda.dynaparm.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		};
		
	}
})(angular);